Home:ALL Converter>Usage of formGroupDirective for reset form - Angular reactive form

Usage of formGroupDirective for reset form - Angular reactive form

Ask Time:2019-01-31T02:21:41         Author:Roy

Json Formatter

I am trying to find best way to reset angular reactive form. I'm bit confused for reset reactive forms and not able to find which method is for template driven forms and which is reactive forms. Now I've used 'formGroupDirective' to reset but I'm getting console error like below. enter image description here

this is how I have used formGroupDirective for reset.

Template file:

<form 
  ...
  #formDirective="formGroupDirective" 
>

Author:Roy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54447095/usage-of-formgroupdirective-for-reset-form-angular-reactive-form
Juan Martin :

If you're using Reactive Forms you can simply use the reset() method on the FormGroup to clear all the form values and mark the controls as pristine again, as it was pointed out already. But you can also use FormGroupDirective to use resetForm(), as this will mark the submitted property of the form as false, something the regular reset() method won't do. \n\nThis is especially helpful if you're using Angular Material, as the default ErrorStateMatcher will check whether the form has been submitted as one of the conditions to display the form error messages. You can use it like this:\n\n@ViewChild(FormGroupDirective) formRef: FormGroupDirective;\n\n\nAnd then:\n\nthis.formRef.resetForm();\n\n\nNo need to add anything to your HTML.\n\nFor more info: https://github.com/angular/angular/pull/10715",
2019-06-09T22:16:03
yy